Telegram Group & Telegram Channel
unfold

Builds a list, using an iterator function and an initial seed value.

The iterator function accepts one argument (seed) and must always return a list with two elements ([value, nextSeed]) or False to terminate.
Use a generator function, fn_generator, that uses a while loop to call the iterator function and yield the value until it returns False.
Use a list comprehension to return the list that is produced by the generator, using the iterator function.

CODE:

def unfold(fn, seed):
def fn_generator(val):
while True:
val = fn(val[1])
if val == False: break
yield val[0]
return [i for i in fn_generator([None, seed])]

f = lambda n: False if n > 50 else [-n, n + 10]
INPUT:
unfold(f, 10)

OUTPUT:
[-10, -20, -30, -40, -50]

Share and Support
@Python_Codes



tg-me.com/python_codes/182
Create:
Last Update:

unfold

Builds a list, using an iterator function and an initial seed value.

The iterator function accepts one argument (seed) and must always return a list with two elements ([value, nextSeed]) or False to terminate.
Use a generator function, fn_generator, that uses a while loop to call the iterator function and yield the value until it returns False.
Use a list comprehension to return the list that is produced by the generator, using the iterator function.

CODE:

def unfold(fn, seed):
def fn_generator(val):
while True:
val = fn(val[1])
if val == False: break
yield val[0]
return [i for i in fn_generator([None, seed])]

f = lambda n: False if n > 50 else [-n, n + 10]
INPUT:
unfold(f, 10)

OUTPUT:
[-10, -20, -30, -40, -50]

Share and Support
@Python_Codes

BY Python Codes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_codes/182

View MORE
Open in Telegram


Python Codes Telegram | DID YOU KNOW?

Date: |

Start with a fresh view of investing strategy. The combination of risks and fads this quarter looks to be topping. That means the future is ready to move in.Likely, there will not be a wholesale shift. Company actions will aim to benefit from economic growth, inflationary pressures and a return of market-determined interest rates. In turn, all of that should drive the stock market and investment returns higher.

Telegram has exploded as a hub for cybercriminals looking to buy, sell and share stolen data and hacking tools, new research shows, as the messaging app emerges as an alternative to the dark web.An investigation by cyber intelligence group Cyberint, together with the Financial Times, found a ballooning network of hackers sharing data leaks on the popular messaging platform, sometimes in channels with tens of thousands of subscribers, lured by its ease of use and light-touch moderation.Python Codes from vn


Telegram Python Codes
FROM USA